Skip to content

fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852

Merged
BYK merged 3 commits intomainfrom
cursor/sentry-cli-bug-fixes-4540
May 1, 2026
Merged

fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env#852
BYK merged 3 commits intomainfrom
cursor/sentry-cli-bug-fixes-4540

Conversation

@cursor
Copy link
Copy Markdown
Contributor

@cursor cursor Bot commented Apr 27, 2026

Summary

DSN detection only matched SENTRY_DSN=... in .env files and process.env.SENTRY_DSN at runtime. Framework-prefixed variants like NEXT_PUBLIC_SENTRY_DSN, REACT_APP_SENTRY_DSN, VITE_SENTRY_DSN were not detected, causing sentry init to create a new project instead of reusing the existing one.

Trigger: Having a DSN via NEXT_PUBLIC_SENTRY_DSN in .env.local and running sentry init.

Changes

  • env.ts: Added a FRAMEWORK_ENV_PREFIXES allowlist and a loop that checks SENTRY_DSN first, then framework-prefixed variants. Invalid DSNs are skipped (falls through to next candidate). The matched var name is passed as sourcePath so source descriptions are accurate.
  • env-file.ts: Replaced the open ^(?:\w+_)?SENTRY_DSN regex with one built from the shared FRAMEWORK_ENV_PREFIXES allowlist, so both detection paths match exactly the same set of variable names.
  • detector.ts: getDsnSourceDescription now uses dsn.sourcePath for the "env" case (falls back to SENTRY_DSN when unset).
  • types.ts: Updated sourcePath JSDoc to document dual semantics (env var name for "env" source, file path for file-based sources).
  • Tests: Added test/lib/dsn/env.test.ts (12 tests covering all framework vars, canonical priority, invalid-DSN skip-through, empty values). Added framework-prefix match/reject tests to env-file.test.ts. Added getDsnSourceDescription test for env source with sourcePath.

Fixes #820

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Apr 27, 2026

PR Preview Action v1.8.1

QR code for preview link

🚀 View preview at
https://cli.sentry.dev/_preview/pr-852/

Built to branch gh-pages at 2026-05-01 23:24 UTC.
Preview will be ready when the GitHub Pages deployment is complete.

…ocess.env (#820)

`sentry init` was not detecting DSNs supplied via framework-specific
environment variables like `NEXT_PUBLIC_SENTRY_DSN`, `REACT_APP_SENTRY_DSN`,
`VITE_SENTRY_DSN`, etc. This caused init to create a new Sentry project
instead of reusing the existing one when the DSN was provided through a
framework-prefixed env var.

The .env file scanner (env-file.ts) only matched the exact pattern
`SENTRY_DSN=...`. The runtime env detection (env.ts) only checked
`process.env.SENTRY_DSN`.

Fix:
- Expanded the .env file regex from `^SENTRY_DSN` to `^(?:\w+_)?SENTRY_DSN`
  to match framework-prefixed variants (NEXT_PUBLIC_, REACT_APP_, VITE_, etc.)
- Added explicit checks for the 5 most common framework-prefixed env vars
  in the runtime env detection, checked after the canonical SENTRY_DSN

Co-authored-by: Miguel Betegón <miguelbetegongarcia@gmail.com>
@BYK BYK force-pushed the cursor/sentry-cli-bug-fixes-4540 branch from f9e2abb to 788ce91 Compare May 1, 2026 21:46
@BYK BYK changed the title fix: three bug fixes — span list cross-project, init ANSI escapes, DSN env detection fix(dsn): detect framework-prefixed DSN env vars in .env files and process.env May 1, 2026
@BYK BYK marked this pull request as ready for review May 1, 2026 21:47
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 1, 2026

Codecov Results 📊

6554 passed | Total: 6554 | Pass Rate: 100% | Execution Time: 0ms

📊 Comparison with Base Branch

Metric Change
Total Tests 📈 +24
Passed Tests 📈 +24
Failed Tests
Skipped Tests

All tests are passing successfully.

✅ Patch coverage is 100.00%. Project has 13294 uncovered lines.
✅ Project coverage is 76.1%. Comparing base (base) to head (head).

Coverage diff
@@            Coverage Diff             @@
##          main       #PR       +/-##
==========================================
+ Coverage    76.02%    76.10%    +0.08%
==========================================
  Files          296       296         —
  Lines        55640     55618       -22
  Branches         0         0         —
==========================================
+ Hits         42298     42324       +26
- Misses       13342     13294       -48
- Partials         0         0         —

Generated by Codecov Action

Comment thread src/lib/dsn/env.ts Outdated
Comment thread src/lib/dsn/env.ts
Address Bugbot review:
- Invalid DSN in SENTRY_DSN no longer causes premature return; the
  loop continues to check framework-prefixed vars.
- The actual env var name (e.g. NEXT_PUBLIC_SENTRY_DSN) is passed as
  sourcePath so getDsnSourceDescription reports the correct variable.
Copy link
Copy Markdown
Contributor Author

@cursor cursor Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Fix All in Cursor

❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.

Reviewed by Cursor Bugbot for commit 30b4cc3. Configure here.

Comment thread src/lib/dsn/env-file.ts Outdated
…tests

- Export FRAMEWORK_ENV_PREFIXES from env.ts and build the env-file.ts
  regex from them, so both detection paths match exactly the same set
  of framework-prefixed variable names (no open \w+ regex).
- Update sourcePath JSDoc in types.ts to document dual semantics
  (env var name for 'env' source, file path for file-based sources).
- Update DsnSource doc to mention framework-prefixed env vars.
- Add test/lib/dsn/env.test.ts covering: all framework vars, canonical
  priority, invalid-DSN skip-through, sourcePath propagation.
- Add framework-prefix tests to env-file.test.ts (match + reject).
- Add getDsnSourceDescription test for env source with sourcePath.
@BYK BYK merged commit eb6781c into main May 1, 2026
26 checks passed
@BYK BYK deleted the cursor/sentry-cli-bug-fixes-4540 branch May 1, 2026 23:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

sentry init doesn't detect env vars when deciding whether to reuse a project

2 participants